<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<c:import url="/includes/header.jsp"></c:import>
</head>
<body>

	<!-- 	<div class="container" style="width: 70%" id="switch_container"> -->
	<!-- 		<div class="row"> -->
	<!-- 			<div class="col-sm-4">开通自动接单，再也不错过任何一单</div> -->
	<!-- 			<div class="col-sm-2 col-sm-offset-6" style="float: rigth;"> -->
	<!-- 				<div> -->
	<!-- 					<span>自动接单</span><span><input name="status" type="checkbox" id="switch_status" checked -->
	<!-- 						data-size="small"></span> -->
	<!-- 				</div> -->
	<!-- 			</div> -->
	<!-- 		</div> -->
	<!-- 	</div> -->

	<div class="container" id="order_container">
		<form id="listForm" method="post"
			action="${pageContext.request.contextPath}/admin/order/list.do">
			<div class="row">
				<div class="col-sm-6" id="buttonGroup">
					<input type="button"
						class="btn btn-defalut ${pageModel.queryMap['type']=='all'||pageModel.queryMap['type']==''?'btn-info':'' } "
						value="所有平台" name="all" style="margin-left: 10px"> <input
						type="button" name="ele"
						class="btn btn-defalut ${pageModel.queryMap['type']=='ele'?'btn-info':'' }"
						value="饿了么" style="margin-left: 10px"> <input
						type="button"
						class="btn btn-defalut ${pageModel.queryMap['type']=='mt'?'btn-info':'' }"
						value="美团外卖" name="mt" style="margin-left: 10px">
				</div>

				<div class="col-sm-3 col-sm-offset-3">
					<input type="hidden" name="status"
						value="${pageModel.queryMap['status']}" id="orderStatus">
					<input type="hidden" name="type"
						value="${pageModel.queryMap['type']}" id="plType"> <input
						type="text" name="queryMap['phone']" class="form-control search"
						value="${pageModel.queryMap['phone']}" placeholder="完整手机号或者手机号后四位">
					<input type="hidden" name="book"
						value="${pageModel.queryMap['book']}" id="orderBook">
				</div>
			</div>

			<div class="row">
				<c:forEach items="${list}" var="s">
					<table class="table table-bordered table-hover" id="table_${s.id }"
						style="text-align: left; margin-top: 20px; background-color: white;">
						<tr style="background-color: ${s.type=='ele'?'#0c9dd9':'#fbc700'}">
							<td><div style="font-size: 18px" class="col-sm-9">
									<span>${s.type=="ele"?"饿了么":"美团外卖" }#${s.daySn }</span>
									<c:if test="${s.book==false}">
										<span style="margin-left: 10px">尽快送达 </span>
									</c:if>
									<c:if test="${s.book==true}">
										<span style="margin-left: 10px">期望 <fmt:formatDate
												value="${s.deliverTime}" pattern="HH:mm" />送达
										</span>
									</c:if>

								</div>
								<div style="font-size: 16px" class="col-sm-3">
									<c:if test="${s.status=='unprocessed'}">
										<span style="float: right">等待接单</span>
									</c:if>
									<c:if test="${s.status=='valid'}">
										<span style="float: right">已接单</span>
									</c:if>
									<c:if test="${s.status=='refunding'}">
										<span style="float: right">退款处理中</span>
									</c:if>
									<c:if test="${s.status=='invalid'}">
										<span style="float: right">已取消 </span>
									</c:if>
									<c:if test="${s.status=='settled'}">
										<span style="float: right">已完成 </span>
									</c:if>

								</div></td>

						</tr>
						<tr>
							<td>
								<div style="font-size: 16px" class="col-sm-9">
									<p>${s.consigness }【${s.phone}】</p>
									<p>${s.deliverPoi}【${s.dist}km】</p>
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="font-size: 16px" class="col-sm-9">
									<p>
										<span>商品(${s.totalQuantity })</span>
									</p>
								</div>
								<div style="font-size: 16px" class="col-sm-3">
									<a style="float: right; cursor: pointer;" class="open">展开<span
										class="caret"></span></a>

								</div>
								<div style="display: none" class="col-sm-9">
									<table class="table " style="text-align: left">
										<tbody>
											<c:forEach items="${s.items }" var="item">
												<tr>
													<td width="70%">${item.name }</td>
													<td>×${item.quantity }</td>
													<td>${item.price*item.quantity }</td>
												</tr>
											</c:forEach>
										</tbody>
										<tbody>
											<tr>
												<td width="70%" colspan="3">其他</td>
											</tr>
											<c:forEach items="${s.orderGroups }" var="g">
												<tr>
													<td width="70%">${g.name}</td>
													<td></td>
													<td>${g.price}</td>
												</tr>
											</c:forEach>
										</tbody>
										<tbody>
											<tr>
												<td width="70%">配送费</td>
												<td></td>
												<td>${s.deliverFee}</td>
											</tr>
										</tbody>
									</table>
								</div>
							</td>
						</tr>
						<tr>
							<td><div class="col-sm-9">
									<p>
										<span>客户实付</span><span style="float: right">${s.totalPrice }</span>
									</p>
									<p>
										<span>本单预计收入</span><span style="float: right">${s.income }</span>
									</p>
								</div></td>
						</tr>
						<tr>
							<td><div class="col-sm-7">
									<p>
										<span>下单时间:<fmt:formatDate value="${s.activeAt}"
												pattern="yyyy-MM-dd HH:mm:ss" /></span>
									</p>
									<p>
										<span>单号:${s.plfOrderId }</span>
									</p>
								</div>
								<div class="col-sm-5">

									<c:if test="${s.status=='unprocessed'}">
										<div class="unprocessed" data-orderId="${s.id }"
											data-type="${s.type }">
											<p>
												<!-- book 预定 -->
												<input type="button" class="btn btn-default" value="立即接单"
													data-book="${s.book}" data-type="confirm"> <input
													type="button" class="btn btn-default" value="打印订单"
													data-type="print"> <input type="button"
													class="btn btn-default" value="取消接单" data-type="canel">
											</p>
										</div>
									</c:if>
									<c:if test="${s.status=='valid'}">
										<div class="valid" data-orderId="${s.id }"
											data-type="${s.type }">
											<p>
												<!-- thirdDeliver 第三方配送 -->
												<c:if test="${s.thirdDeliver==true&&s.isDeliver==false}">
													<input type="button" class="btn btn-default" value="一键发单"
														data-type="send">
												</c:if>
												<input type="button" class="btn btn-default" value="打印订单"
													data-type="print"> <input type="button"
													class="btn btn-default" value="取消订单" data-type="canel">
											</p>
										</div>
									</c:if>
									<c:if test="${s.status=='refunding'}">
										<div class="refunding" data-orderId="${s.id }"
											data-type="${s.type }">
											<p>
												<input type="button" class="btn btn-default" value="同意退单"
													data-type="agree"> <input type="button"
													class="btn btn-default" value="不同意退单" data-type="disagree">
											</p>
										</div>
									</c:if>
									<c:if test="${s.status=='apply_cancel'}">
										<div class="refunding" data-orderId="${s.id }"
											data-type="${s.type }">
											<p>
												<input type="button" class="btn btn-default" value="同意取消"
													data-type="agree"> <input type="button"
													class="btn btn-default" value="拒绝取消" data-type="disagree">
											</p>
										</div>
									</c:if>
									
									<c:if test="${s.status=='invalid'}">
										<div class="invalid" data-orderId="${s.id }"
											data-type="${s.type }">
											<p>
												<input type="button" class="btn btn-default" value="打印订单"
													data-type="print">
											</p>
										</div>
									</c:if>
									<c:if test="${s.status=='settled'}">
										<div class="settled" data-orderId="${s.id }"
											data-type="${s.type }">
											<p>
												<input type="button" class="btn btn-default" value="打印订单"
													data-type="print">
											</p>
										</div>
									</c:if>
								</div></td>
						</tr>
					</table>
				</c:forEach>
				<c:import url="/includes/page.jsp"></c:import>
			</div>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {
		var loadpng = $("#loadPng");
		$("#buttonGroup").find("input[type='button']").click(function() {
			var name = $(this).attr("name");
			if (name == "all") {
				$("#plType").val("");
			} else {
				$("#plType").val(name);
			}
			$("#listForm").submit();
		});

		$(".open").click(function() {
			$(this).toggleClass("dropup");
			if ($(this).hasClass("dropup")) {
				$(this).parent().next().show();
			} else {
				$(this).parent().next().hide();
			}
		});
		$(".search").keydown(function(e) {
			if (e.which == "13") {
				$("#listForm").submit();
			}
		});

		$(".unprocessed,.valid,.refunding,.invalid,.settled").find("input")
				.click(
						function() {
							var orderId = $(this).parent().parent().attr(
									"data-orderId");
							var dataType = $(this).attr("data-type");
							if (dataType == "print") {
								getOrderToPrint(orderId);
							} else {
								confirmHandle(orderId, dataType);
							}

						});

		//订单操作处理
		var confirmHandle = function(orderId, dataType) {
			var data = {
				"orderId" : orderId,
				"dataType" : dataType
			};
			var url = '${pageContext.request.contextPath}/admin/order/handle.do';
			$.ajax({
				type : "post",
				dataType : "json",
				data : data,
				url : url,
				async : false,
				success : function(data) {
					if (data.code == 0) {
						$("#table_" + orderId).remove();
						// 						bootbox.alert("操作成功", function() {
						// 							window.location.reload();
						// 						});

					} else {
						bootbox.alert("网络不稳定,运行失败");
					}
				},
				error : function() {
					bootbox.alert("网络不稳定,运行失败");
				}
			});
		}

		//获取订单信息进行打印
		var getOrderToPrint = function(orderId) {
			var data = {
				"orderId" : orderId,
			};
			var url = '${pageContext.request.contextPath}/admin/order/print.do';
			$.ajax({
				type : "post",
				dataType : "json",
				data : data,
				url : url,
				async : false,
				success : function(data) {
					if (data.code == "0") {
						var list = new Array();
						list.push(data.data);
						var str = JSON.stringify(list);
						top.nativePrint(str);
					} else {
						bootbox.alert("网络不稳定,运行失败");
					}
				},
				error : function() {
					bootbox.alert("网络不稳定,运行失败");
				}
			});
		}
	});
</script>
